import { Component, OnInit } from '@angular/core';
import { FormGroup, Validators, FormControl, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';
import { LoginPage } from './model/login.model';
import { LoginService } from './service/login.service';
import { CommonService } from '../../service/common.service';

@Component({
    templateUrl: 'login-lawcase.component.html',
    styleUrls: ['login-lawcase.component.scss'],
    providers: [LoginService]
})
export class LoginLawcaseComponent implements OnInit {
    public form: FormGroup;
    public username = new FormControl('', Validators.required);
    public password = new FormControl('', Validators.required);

    public processingLogin = false;
    public statusMessage = '';
    public showQrcode: boolean = true;
    public showBgImageDetail: boolean = false;
    public appUrl: string = '';
    public currentBackgroundIndex: number = 0;
    public backgrounds: LoginPage[];
    public currentBackgroundPage = new LoginPage();

    constructor(
        private fb: FormBuilder,
        private router: Router,
        private commonService: CommonService,) { };

    public ngOnInit() {
        /**绑定登录表单 */
        this.form = this.fb.group({
            'username': this.username,
            'password': this.password
        });
    }

    /** 用户登录 */
    public login() {
        //"bhsfj" "bhsfj123456"
        this.processingLogin = true;
        this.statusMessage = "系统登录中，请稍候...";
        let formValue = this.form.value;
        setTimeout(()=>{
            if(formValue.username == "bhsfj" 
                && formValue.password == "bhsfj123456"){
                    this.statusMessage = "登录成功，系统正在初始化，请稍候...";
                    this.commonService.login().subscribe(data =>{
                        this.router.navigateByUrl("lawcase");
                    });
            }else{
                this.processingLogin = false;
                this.statusMessage = "用户名或密码错误!"; 
            }
        }, 800);
    }

}

